#include "book.h"

using namespace std;

Book::Book(const string &attr01, const string &attr02, const int &attr03,
           const string &attr04, const int &attr05, const string &attr06,
           const int &attr07, const bool &attr08) {

    author = attr01;
    title = attr02;
    yearOfWriting = attr03;
    publisher = attr04;
    yearOfPublication = attr05;
    ISBN = attr06;

    bookId = newBookId(attr01, attr02, attr03);

    pieceInStock = attr07;
    pieceSum = attr07;
    toRent = attr08;
}

Book::Book(const string &attr01, const string &attr02, const int &attr03,
           const string &attr04, const int &attr05, const string &attr06,
           const string &attr07, const int &attr08, const int &attr09, const bool &attr10) {

    author = attr01;
    title = attr02;
    yearOfWriting = attr03;
    publisher = attr04;
    yearOfPublication = attr05;
    ISBN = attr06;
    bookId = attr07;
    pieceInStock = attr08;
    pieceSum = attr09;
    toRent = attr10;
}

string Book::newBookId(const string &a, const string &b, const int &c) {
    string h;
    stringstream ss;
    ss << c;

    h = a.substr(0,3) + b.substr(0,5) + ss.str();
    return h;
}

ifstream& operator>>(ifstream& f, Book& df) {
    string tmp;

    getline(f, df.author);
    getline(f, df.title);
    f >> df.yearOfWriting;
    getline(f, tmp);
    getline(f, df.publisher);
    f >> df.yearOfPublication >> df.ISBN >> df.bookId >> df.pieceInStock >> df.pieceSum >> df.toRent;
    getline(f, tmp);
    return f;
}

ofstream& operator<<(ofstream& f, const Book& df) {
    f <<  df.author << endl
      << df.title << endl
      << df.yearOfWriting << endl
      << df.publisher << endl
      << df.yearOfPublication << endl
      << df.ISBN << endl
      << df.bookId << endl
      << df.pieceInStock << endl
      << df.pieceSum << endl
      << df.toRent << endl;

    return f;
}

ostream& operator<<(ostream& f, const Book& df) {
    f << "Author: " <<  df.author << endl
      << "Title: " << df.title << endl
      << "Year: " << df.yearOfWriting << endl
      << "Publisher: " << df.publisher << endl
      << "Year: " << df.yearOfPublication << endl
      << "ISBN: " << df.ISBN << endl
      << "Book ID: " << df.bookId << endl
      << "Piece in the stock: " << df.pieceInStock << endl
      << "Piece summation: " << df.pieceSum << endl
      << "Is Rent?: ";

    df.toRent?(f << "Yes"):(f << "No");

    f << std::endl;

    return f;
}

bool operator==(const Book& a, const Book& b) {
    return a.bookId == b.bookId;
}

bool operator>(const Book& a, const Book& b) {
    if(a.author != b.author)
        return a.author > b.author;
    else
        return a.title > b.title;
}

bool operator<(const Book& a, const Book& b) {
    if(a.author != b.author)
        return a.author < b.author;
    else
        return a.title < b.title;
}
